package cn.shop.dys.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;


import com.bumptech.glide.Glide;

import cn.shop.dys.R;
import cn.shop.dys.constant.SPConstant;
import cn.shop.dys.util.SPUtil;

public class GuideActivity extends AppCompatActivity {

    private ViewPager vpGuide;
    int[]pics=new int[]{R.drawable.guid_view1,
            R.drawable.guid_view2,
            R.drawable.guid_view3
    };
    private ViewPager.OnPageChangeListener listener=new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position<2){
                if (btn_enter != null) {
                    btn_enter.setVisibility(View.INVISIBLE);
                    btn_enter.setEnabled(false);
                }
            }else {
                if (btn_enter != null) {
                    btn_enter.setEnabled(true);
                    btn_enter.setVisibility(View.VISIBLE);
                }
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
    private Button btn_enter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        vpGuide = (ViewPager) findViewById(R.id.vp_guide);
        btn_enter = (Button) findViewById(R.id.btn_enter);
        vpGuide.addOnPageChangeListener(listener);
        init();

    }


    private void init() {

        vpGuide.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return pics.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
               container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView iv=new ImageView(GuideActivity.this);

                Glide.with(container.getContext()).load(pics[position]).centerCrop().crossFade().into(iv);
                container.addView(iv);
                return iv;
            }
        });
    }

    public void openActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        SPUtil.put(this, SPConstant.ISNOTFIRSTIN, true);
    }

    @Override
    protected void onDestroy() {
        vpGuide.removeOnPageChangeListener(listener);
        pics=null;
        listener=null;
        vpGuide.removeAllViews();
        super.onDestroy();
    }

    public void goToMain(View view) {
        openActivity(MainActivity2.class);
    }
}
